<?php

/**
 * 73YC 首页文件
 * ============================================================================
 * * 版权所有 20013-* 梅州络客网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.alork.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2013-10-19 06:29:08Z liubo $
*/

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . '/includes/funTvd.php');
@ini_set('display_errors',        1);

/* 是否有id参数 */
if (empty($_REQUEST['id'])) {
    header('location: index.php');
    exit;
} else {
    $id = intval($_REQUEST['id']);
}


/* 收藏文章 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'share') {
    $sql = "UPDATE " . $GLOBALS['ecs']->table('tvd') . " SET share = share + 1 WHERE tvd_id = '$id'";
            $GLOBALS['db']->query($sql);
    $result['error']   = 0;
    $result['message']   = '成功分享';
    echo JSON()->encode($result);
    exit();
}

/* 收藏文章 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'collect') {
    if (!empty($_SESSION['user_id'])) {
        $sql = "SELECT count(*) AS iscollect FROM " . $GLOBALS['ecs']->table('tvd_collect') . " WHERE tvd_id = '$id' AND user_id = '" . $_SESSION['user_id'] . "'";
        $iscollect = $GLOBALS['db']->getOne($sql);
        if (empty($iscollect)) {
            $sql = "INSERT INTO " . $GLOBALS['ecs']->table('tvd_collect') . " (tvd_id, user_id) VALUES('$id', '$_SESSION[user_id]')";
            $GLOBALS['db']->query($sql);
            $sql = "UPDATE " . $ecs->table('tvd') . " SET praise = praise + 1 WHERE tvd_id = '$id'";
            $db->query($sql);
            $result['error']   = 0;
            $result['message']   = '关注成功';
        } else {
            $result['error']   = 0;
            $result['message']   = '之前已经关注';
        }
    } else {
            $result['error']   = 1;
            $result['message']   = '请登录后再关注';
    }
    echo JSON()->encode($result, false);
    exit();
}

/* 取消收藏 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'uncollect') {
    if (!empty($_SESSION['user_id'])) {
        $sql = "SELECT count(*) AS iscollect FROM " . $GLOBALS['ecs']->table('tvd_collect') . " WHERE tvd_id = '$id' AND user_id = '" . $_SESSION['user_id'] . "'";
        $iscollect = $GLOBALS['db']->getOne($sql);
        if (empty($iscollect)) {
            $result['error']   = 1;
            $result['message']   = '收藏信息出错';
        } else {
            $sql = "DELETE FROM " . $GLOBALS['ecs']->table('tvd_collect') . " WHERE tvd_id = '$id' AND user_id = '" . $_SESSION['user_id'] . "'";
            $GLOBALS['db']->query($sql);
            $result['error']   = 0;
        }
    } else {
            $result['error']   = 1;
            $result['message']   = '请登录后再取消收藏';
    }
    echo JSON()->encode($result);
    exit();
}

/* 评论文章 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'comment') {
    assign_template();
    include_once('includes/cls_json.php');
    $json  = new JSON;
    if (!empty($_SESSION['user_id']) && !empty($_REQUEST['comment'])) {
        $comment = strip_tags($_REQUEST['comment']);
        $at_user = strip_tags(trim($_REQUEST['at_user']));
        $sql = "INSERT INTO egg_tvd_comment (tvd_id, user_id, user_name, ip_address, content, at_user) VALUES('$id', '$_SESSION[user_id]', '$_SESSION[user_name]', '$_SESSION[last_ip]', '$comment', '$at_user')";
        $iscomment = $GLOBALS['db']->query($sql);
        $result['error']   = 0;
        $result['message']   = '评论成功';
        $smarty->assign('commentList', getTvdComment($id, 0, 1));// 内容信息
        $result['content']   = $smarty->fetch("tvd/comtList.lbi");
    } else {
            $result['error']   = 1;
            $result['message']   = '请登录后再评论';
    }
    echo $json->encode($result);
    exit();
}

/* 评论列表 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'commentList') {
    assign_template();
    $smarty->assign('id', $id);// ID
    /* AJAX获取评论列表 */
    if ($_REQUEST['isAjax'] == 1) {
        include_once('includes/cls_json.php');
        $json  = new JSON;
        $commentList = getTvdComment($id, 3, 100);
        $smarty->assign('commentList', $commentList);// 内容信息
        if ($commentList) {
            $result['error'] = 0;
            $result['content'] = $smarty->fetch("tvd/comtList.lbi");
        } else {
            $result['error'] = 1;
            $result['message'] = '已全部加载！END';
        }
        echo $json->encode($result);
        exit();
    }
    $smarty->assign('act', 'commentList');
    $smarty->display("tvd.dwt");
}

/* 深度垂直内容页信息 */
else {
    assign_template();
    $sql = "UPDATE " . $ecs->table('tvd') . " SET click = click + 1 WHERE tvd_id = '$id'";
    $db->query($sql);
    $tvdInfo = getTvdInfo($id);

    /* 获取关联商品 */
    $sql = "SELECT goods_id FROM egg_tvd_goods WHERE tvd_id = {$id}";
    $goodsId = DB()->getCol($sql);
    if (!empty($goodsId)) {
        $goodsId = implode(',', $goodsId);
        $sql = "SELECT goods_thumb, goods_name, goods_id, shop_price FROM egg_goods WHERE goods_id IN ($goodsId)";
        $goodsList = DB()->getAll($sql);
        if (!empty($goodsList)) {
            foreach ($goodsList as $key => $value) {
                $value['url'] = getUrl('goods', $value);
                $goodsList[$key] = $value;
            }
            $smarty->assign('goodsList', $goodsList);// 内容信息
        }
    }
    /* 随机获取三篇推荐文章 */
    $sql = "SELECT list_img, title, tvd_id FROM egg_tvd WHERE is_open = 1 ORDER BY RAND() LIMIT 3";
    $tvdList = DB()->getAll($sql);
    foreach ($tvdList as $key => $value) {
        $value['url'] = getUrl('tvd', $value['tvd_id']);
        $tvdList[$key] = $value;
    }

    if (!empty($tvdInfo['keywords'])) {
        $smarty->assign('keyList', explode(',', $tvdInfo['keywords']));// 关键词
    }
    $smarty->assign('tvdList', $tvdList);// 推荐
    $smarty->assign('commentList', getTvdComment($id));// 评论
    $smarty->assign('page_title', $tvdInfo['title']);// 页头
    $smarty->assign('tvdInfo', $tvdInfo);// 内容信息
    $smarty->assign('id', $id);// ID
    $smarty->assign('nav', true); // 是否显示导航
    $smarty->display("tvd/tvd.dwt");
}

